package com.example.umbrella.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.umbrella.R;
import com.example.umbrella.model.CharacteristicModel;
import com.example.umbrella.util.DataConverter;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;
import com.google.android.material.chip.Chip;

import java.util.ArrayList;
import java.util.List;

public class CharacteristicAdapter extends RecyclerView.Adapter<CharacteristicAdapter.ViewHolder> {
    private List<CharacteristicModel> characteristics = new ArrayList<>();
    private OnCharacteristicWriteListener writeListener;

    public interface OnCharacteristicWriteListener {
        void onWriteClick(CharacteristicModel characteristic);
    }

    public void setOnCharacteristicWriteListener(OnCharacteristicWriteListener listener) {
        this.writeListener = listener;
    }

    public void setCharacteristics(List<CharacteristicModel> characteristics) {
        this.characteristics = characteristics;
        notifyDataSetChanged();
    }

    public void updateCharacteristicValue(String uuid, byte[] value) {
        for (int i = 0; i < characteristics.size(); i++) {
            CharacteristicModel characteristic = characteristics.get(i);
            if (characteristic.getUuid().equals(uuid)) {
                characteristic.setRawValue(value);
                notifyItemChanged(i);
                break;
            }
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_characteristic, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CharacteristicModel characteristic = characteristics.get(position);
        holder.bind(characteristic, writeListener);
    }

    @Override
    public int getItemCount() {
        return characteristics.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView uuidText;
        private final TextView valueText;
        private final MaterialButton writeButton;
        private final Chip propertyTag;
        private final MaterialCardView cardView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            uuidText = itemView.findViewById(R.id.characteristic_uuid);
            valueText = itemView.findViewById(R.id.characteristic_value);
            writeButton = itemView.findViewById(R.id.btn_write);
            propertyTag = itemView.findViewById(R.id.property_tag);
            cardView = (MaterialCardView) itemView;
        }

        public void bind(CharacteristicModel characteristic, OnCharacteristicWriteListener listener) {
            Context context = itemView.getContext();
            uuidText.setText(characteristic.getUuid());
            
            // 设置属性标签和卡片背景
            if (characteristic.isReadable() && characteristic.isWritable()) {
                propertyTag.setText("可读写");
                propertyTag.setChipBackgroundColorResource(R.color.tag_writable);
                cardView.setCardBackgroundColor(context.getColor(R.color.writable_card_background));
                // 显示16进制对应的ASCII
                String hexValue = DataConverter.bytesToHex(characteristic.getRawValue());
                valueText.setText(DataConverter.hexToAscii(hexValue));
            } else if (characteristic.isReadable()) {
                propertyTag.setText("可读");
                propertyTag.setChipBackgroundColorResource(R.color.tag_readable);
                cardView.setCardBackgroundColor(context.getColor(R.color.readable_card_background));
                // 显示16进制对应的ASCII
                String hexValue = DataConverter.bytesToHex(characteristic.getRawValue());
                valueText.setText(DataConverter.hexToAscii(hexValue));
            } else if (characteristic.isWritable()) {
                propertyTag.setText("可写");
                propertyTag.setChipBackgroundColorResource(R.color.tag_writable);
                cardView.setCardBackgroundColor(context.getColor(R.color.writable_card_background));
                valueText.setText(DataConverter.bytesToHex(characteristic.getRawValue()));
            }
            
            // 设置写入按钮
            if (characteristic.isWritable()) {
                writeButton.setVisibility(View.VISIBLE);
                writeButton.setOnClickListener(v -> {
                    if (listener != null) {
                        listener.onWriteClick(characteristic);
                    }
                });
            } else {
                writeButton.setVisibility(View.GONE);
            }
        }
    }
}